package com.wh.generator;


import cn.hutool.core.util.ArrayUtil;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;

public class StaticGenerator {

    //递归实现目录拷贝
    public static void copyFiles(String inputPath, String outputPath) {
        File inputFile = new File(inputPath);
        File outputFile = new File(outputPath);

        try {
            copyFiles(inputFile, outputFile);
        }catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static void copyFiles(File inputFile, File outputFile) throws IOException {

        if(inputFile.isDirectory()){

            //如果是目录就复制到输出目录下
            File destOutputFile = new File(outputFile, inputFile.getName());
            if(!destOutputFile.exists()){
                destOutputFile.mkdirs();  //创建对应目录
            }
    
            //获取目录下所有文件
            File[] files = inputFile.listFiles();
            //无子文件直接结束
            if(ArrayUtil.isEmpty(files)) {
                return;
            }
            //递归调用
            for (File file : files) {
                copyFiles(file, destOutputFile);
            }
        }else{

            //计算目标文件名
            Path destPath = outputFile.toPath().resolve(inputFile.getName());

            Files.copy(inputFile.toPath(), destPath, StandardCopyOption.REPLACE_EXISTING);
        }
    }
}
